package com.geeksaga.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.geeksaga.common.util.CacheUtil;
import com.geeksaga.dao.BbsManagerDao;
import com.geeksaga.dao.BbsTypeDao;
import com.geeksaga.dao.TablePartitionDao;
import com.geeksaga.dao.TableRowCountDao;
import com.geeksaga.domain.BbsType;
import com.geeksaga.domain.TablePartition;
import com.geeksaga.domain.TableRowCount;

/**
 * @author newbie, casper0402
 * @since 0.1
 */
@Service
public class BbsTypeServiceImpl implements BbsTypeService {
	
    @Autowired private BbsTypeDao bbsTypeDao;
    @Autowired private BbsManagerDao bbsManagerDao;
    @Autowired private TableRowCountDao tableRowCountDao;
    @Autowired private TablePartitionDao tablePartitionDao;
    
    /*
     * (non-Javadoc)
     * @see com.geeksaga.service.BbsTypeService#add(com.geeksaga.domain.BbsType)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public BbsType add(BbsType bbsType) throws DataAccessException {
    	TableRowCount tableRowCount = new TableRowCount();
    	TablePartition tablePartition = new TablePartition();
    	
		bbsType.setCategoryYn("0");
		bbsType.setBbsUseYn("1");
		bbsType.setListYn("1");
    	bbsType.setBbsType("A00101");
		bbsType.setBbsSkin("default");
		
		bbsType.setListGrade("0");
		bbsType.setViewGrade("0");
		bbsType.setWriteGrade("0");
		bbsType.setMemoGrade("0");
		bbsType.setMemoYn("0");
		
		
    	tableRowCount.setTableName("PW_BBS");
    	tableRowCount.setRowId(bbsType.getBbsId());
    	
    	tablePartition.setTableName("PW_BBS");
    	tablePartition.setRowId(bbsType.getBbsId());
    	
    	bbsTypeDao.add(bbsType);
    	tableRowCountDao.addTableRowCountDao(tableRowCount);
    	tablePartitionDao.add(tablePartition);
    	findCacheByBbsId(bbsType.getBbsId());
    	
    	return bbsType;
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsTypeService#loadCache()
     */
    public void loadCache() throws DataAccessException {
        List<BbsType> list = bbsTypeDao.list();
        
        Map<String, BbsType> cacheMap = new HashMap<String, BbsType>();
        
        for(BbsType type : list) {
            type.setManager(bbsManagerDao.findByBbsType(type));
            
            cacheMap.put(type.getBbsId(), type);
        }

        if(!cacheMap.isEmpty())
            CacheUtil.put(CacheUtil.CACHE, CacheUtil.KEY_BBS_TYPE, cacheMap);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsTypeService#findCacheByBbsId(java.lang.String)
     */
    public BbsType findCacheByBbsId(String bbsId) {
        Map<String, BbsType> cacheMap = CacheUtil.get(CacheUtil.CACHE, CacheUtil.KEY_BBS_TYPE);
        
        if(cacheMap == null) {
            loadCache();
            
            cacheMap = CacheUtil.get(CacheUtil.CACHE, CacheUtil.KEY_BBS_TYPE);
        }
        
        return (cacheMap != null) ? cacheMap.get(bbsId) : null;
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsTypeService#list()
     */
    public List<BbsType> list() {
    	return bbsTypeDao.list();
    }

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#list(java.util.Map)
	 */
	public List<BbsType> list(Map<String, Object> parameter) {
		return bbsTypeDao.list(parameter);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#count(java.util.Map)
	 */
	public TableRowCount count(Map<String, Object> parameter)
			throws DataAccessException {
		return (TableRowCount)bbsTypeDao.count(parameter);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#bbsUseModify(com.geeksaga.domain.BbsType)
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int bbsUseModify(BbsType bbsType) throws DataAccessException {
		return bbsTypeDao.bbsUseModify(bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#bbsView(com.geeksaga.domain.BbsType)
	 */
	public BbsType bbsView(BbsType bbsType) throws DataAccessException {
		return bbsTypeDao.bbsView(bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#bbsAuthorityModify(com.geeksaga.domain.BbsType)
	 */
	public int bbsAuthorityModify(BbsType bbsType)throws DataAccessException {
		return bbsTypeDao.bbsAuthorityModify(bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#bbsScreenSetUp(com.geeksaga.domain.BbsType)
	 */
	public int bbsScreenSetUp(BbsType bbsType) throws DataAccessException {
		return bbsTypeDao.bbsScreenSetUp(bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.service.BbsTypeService#bbsSecurityUp(com.geeksaga.domain.BbsType)
	 */
	public int bbsSecurityUp(BbsType bbsType) throws DataAccessException {
		return bbsTypeDao.bbsSecuritySetUp(bbsType);
	}
}